package utility.connection;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class HttpParameters {
private Map<String,String> _parameters;
	
	public HttpParameters(){
		_parameters = new HashMap<String,String>();
	}

	public String asEncodedData() {
		StringBuffer data = new StringBuffer();
		for (String each : _parameters.keySet()) {
			String key = encode(each);
			String value = encode(_parameters.get(each));
			data.append(key);
			data.append("=");
			data.append(value);
			data.append("&");
		}
		String result = data.toString();
		if (result.length()>0) result = result.substring(0, result.length()-1);
		return result;
	}

	private String encode(String string) {
		String encoded;
		try {
			encoded = URLEncoder.encode(string, "UTF-8");
			return encoded;
		} catch (UnsupportedEncodingException e) {
			return "";
		}
	}

	public void put(String key, String value) {
		_parameters.put(key,value);
	}

}
